package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.MmrlT;
import cn.e4j.modular.crm.entity.MmrllT;
import cn.e4j.modular.crm.mapper.MmrlTMapper;
import cn.e4j.modular.crm.service.MmrlTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 常见故障维护作业 服务类
 * @author mengyb
 * @date 2019-04-16
 */
@Service
public class MmrlTServiceImpl extends ServiceImpl<MmrlTMapper, MmrlT> implements MmrlTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {
        ShiroUser shiroUser= ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);
    }
    @Transactional
    @Override
    public void add(MmrlT mmrlT, MmrllT mmrllT) {
        ShiroUser shiroUser= ShiroKit.getUser();

        mmrlT.setMMRLENT(shiroUser.getDBC());
        mmrlT.setMMRLUSER(shiroUser.getACCOUNT());
        mmrlT.setMMRLGRUP(shiroUser.getORG_ID());
        mmrlT.setMMRLORIU(shiroUser.getACCOUNT());
        mmrlT.setMMRLORIG(shiroUser.getORG_ID());
        mmrlT.setMMRLTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
        mmrlT.setMMRLDATE(LocalDate.now());

        mmrllT.setMMRLLENT(shiroUser.getDBC());
        mmrllT.setMMRLL001(mmrlT.getMMRL001());
        mmrllT.setMMRLL002(LangUtils.getLang(shiroUser.getLANG()));

        mmrlT.insert();
        mmrllT.insert();

    }
    @Transactional
    @Override
    public void edit(MmrlT mmrlT, MmrllT mmrllT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrlT.setMMRLMODU(shiroUser.getACCOUNT());

        UpdateWrapper<MmrlT> mmrlTUpdateWrapper = new UpdateWrapper<>();

        mmrlTUpdateWrapper.eq("MMRLENT",mmrlT.getMMRLENT());
        mmrlTUpdateWrapper.eq("MMRL001", mmrlT.getMMRL001());

        UpdateWrapper<MmrllT> mmrllTUpdateWrapper=new UpdateWrapper<>();

        mmrllTUpdateWrapper.eq("MMRLLENT",mmrlT.getMMRLENT());
        mmrllTUpdateWrapper.eq("MMRLL001",mmrlT.getMMRL001());

        mmrlT.update(mmrlTUpdateWrapper);
        mmrllT.update(mmrllTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRow(MmrlT mmrlT, MmrllT mmrllT) {
        UpdateWrapper<MmrlT> mmrlTUpdateWrapper = new UpdateWrapper<>();

        mmrlTUpdateWrapper.eq("MMRLENT",mmrlT.getMMRLENT());
        mmrlTUpdateWrapper.eq("MMRL001", mmrlT.getMMRL001());

        UpdateWrapper<MmrllT> mmrllTUpdateWrapper=new UpdateWrapper<>();

        mmrllTUpdateWrapper.eq("MMRLLENT",mmrlT.getMMRLENT());
        mmrllTUpdateWrapper.eq("MMRLL001",mmrlT.getMMRL001());

        mmrlT.delete(mmrlTUpdateWrapper);
        mmrllT.delete(mmrllTUpdateWrapper);
    }
    @Transactional
    @Override
    public void delRows(List<MmrlT> mmrlTList) {
        for (MmrlT mmrlT : mmrlTList
        ) {
            UpdateWrapper<MmrlT> mmrlTUpdateWrapper = new UpdateWrapper<>();

            mmrlTUpdateWrapper.eq("MMRLENT",mmrlT.getMMRLENT());
            mmrlTUpdateWrapper.eq("MMRL001", mmrlT.getMMRL001());

            MmrllT mmrllT=new MmrllT();
            UpdateWrapper<MmrllT> mmrllTUpdateWrapper=new UpdateWrapper<>();

            mmrllTUpdateWrapper.eq("MMRLLENT",mmrlT.getMMRLENT());
            mmrllTUpdateWrapper.eq("MMRLL001",mmrlT.getMMRL001());

            mmrlT.delete(mmrlTUpdateWrapper);
            mmrllT.delete(mmrllTUpdateWrapper);
        }
    }

    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(MmrlT mmrlT) {
        ShiroUser shiroUser= ShiroKit.getUser();
        mmrlT.setMMRLENT(shiroUser.getDBC());
        QueryWrapper<MmrlT> mmrlTQueryWrapper = new QueryWrapper<>();
        mmrlTQueryWrapper.eq("MMRLENT", mmrlT.getMMRLENT());
        mmrlTQueryWrapper.eq("MMRL001", mmrlT.getMMRL001());
        return this.baseMapper.selectCount(mmrlTQueryWrapper)>0?true:false;
    }
}
